import {TMapWX} from '../../libs/TMapWX.js'
import PY from '../../libs/ChinesePY.js'
import q from '../../libs/QuestServer.js'
import mathf from '../../libs/MathF.js'

let TMap = new TMapWX("DPVBZ-NTQ3J-6NGFJ-FNLI7-GYGP6-NRFO4");

Page({
	data: {
		cityGroupId: "G",
		cities: [],
		cityFilterList: [],
		filterCities: [],
		city: "成都市",
		filterCity: false,
		cityKeyRowHeight: 40,
		beginValSearch: false
	},
	onLoad() {
		this.initialCites();
		this.getWeather();
	},
	filterCity: function () {
		this.setData({
			filterCity: true
		})
	},
	selectCity: function (e) {
		this.getWeather(e.target.dataset.city);
		this.setData({
			filterCity: false
		})
	},
	closeCityFilter: function () {
		this.setData({
			filterCity: false
		})
	},
	setStartCityInfo: function (e) {
		this.setData({
			cityGroupId: e.target.dataset.id,
			touchInfo: {
				touch: e.target.dataset.id,
				signal: e.target.dataset.id,
				start: e.touches[0].clientY
			}
		})
	},
	setCityGroupId: function (e) {
		let touchInfo = this.data.touchInfo;
		if (!touchInfo.signal)
			return;
		let increase = Math.ceil((e.touches[0].clientY - touchInfo.start) * 2 / this.data.cityKeyRowHeight);
		let signalNum = touchInfo.signal.charCodeAt(0) + increase;
		this.setData({
			cityGroupId: String.fromCharCode(mathf.clamp(signalNum, 'A'.charCodeAt(0), 'Z'.charCodeAt(0)))
		})
	},
	endkeySearch: function () {
		this.setData({
			touchInfo: {
				touch: false
			}
		});
	},
	beginValSearch: function () {
		this.setData({
			beginValSearch: true
		})
	},
	cancelValSearch: function () {
		this.setData({
			beginValSearch: false
		})
	},
	tryFilterCity: function (e) {
		let key = e.detail.value;
		let filterCity = [];
		if (key){
			let groupId = PY.getPinYin(key)[0];
			let group = this.data.cities.find(x => x.title.toLowerCase() === groupId.toLowerCase());
			if (group){
				let count = 0;
				filterCity = group.lists.filter(x => PY.match(x, key).length > 0 && count++ < 13);
			}
		}
		this.setData({
			filterCity
		})
	},
	getWeather: function (city) {
		TMap.weather({
			city,
			fail: e => {
				this.setData({
					city: this.data.city
				})
			},
			success: ({data}) => {
				let todayDetial = data.forecast_24h[1];
				let curDetial = data.forecast_1h[0];
				let index = data.index;
				this.setData({
					nowTemp: curDetial.degree,
					nowWeather: curDetial.weather,
					dressIndex: index.clothes.info,
					exerciseIndex: index.sports.info,
					washIndex: index.carwash.info,
					travelIndex: index.tourism.info,
					todayStatus:todayDetial.day_weather_short,
					todayWind:todayDetial.day_wind_direction,
					date: todayDetial.time,
					todayTempScop: '今日' + todayDetial.min_degree + '°~' + todayDetial.max_degree + '°',
					city: data.city
				})
			},
		})
	},
	initialCites: function () {
		let keySet = [];
		for (let i = 0; i < 26; i++) {
			keySet[i] = String.fromCharCode('A'.charCodeAt(0) + i);
		}
		
		// keySet.push("*");
		
		q.get("city", ({data}) => {
			let cities = data.city;
			let cityFilterList = [];
			cities.forEach(x => {
				x.lists.forEach(y => {
					cityFilterList.push(y)
				})
			})
			
			this.setData({
				cities,
				cityFilterList,
				keySet
			})
		})
	},
})
